package com.thinkfit.jy.utils;

import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.common.lib.constant.EventBusEvent;
import com.common.lib.manager.DataManager;
import com.common.lib.utils.BaseUtils;
import com.thinkfit.jy.R;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

/**
 * 亮度调节
 */
public class LuminanceViewUtils implements View.OnTouchListener{

    private static LuminanceViewUtils soundViewUtils;
    WindowManager windowManager;
    private AudioManager mAudioManager;
    boolean isFist = true;
    private RelativeLayout.LayoutParams mLayoutParams;
    private View mProgressView;
    private int mMaxHeight;
    private int mMaxVolume;
    private int mCurrentStyle;
    private int mNeedChangeProgress, mMinHeight;
    private HashMap<String, Integer> mMap;
    Context mContext;
    View inflate;

    public static LuminanceViewUtils getInstance(){
        if(soundViewUtils == null){
            soundViewUtils = new LuminanceViewUtils();
        }
        return soundViewUtils;
    }

    public void initView(Context context){
        mContext = context;
        if(windowManager == null){
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(inflate == null){
            inflate = LayoutInflater.from(context).inflate(R.layout.item_brightness, null);
            RelativeLayout relativeLayout = inflate.findViewById(R.id.rl);

            inflate.findViewById(R.id.viewBack).setOnTouchListener(this);
            mProgressView = inflate.findViewById(R.id.viewProgress);
            mCurrentStyle = DataManager.Companion.getInstance().getMainStyle();
            mLayoutParams = (RelativeLayout.LayoutParams) mProgressView.getLayoutParams();
            mMaxHeight = BaseUtils.StaticParams.dp2px(context, 140);
            mMinHeight = BaseUtils.StaticParams.dp2px(context, 0);
            mNeedChangeProgress = BaseUtils.StaticParams.dp2px(context, 130);

            int currentVolume = SystemUtils.getBrightness(mContext);
            //最大亮度
            mMaxVolume = SystemUtils.getMaxBrightness(mContext);

            resetProgressView(mMaxVolume - currentVolume, mMaxVolume);

            relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goneSound();
                }
            });

        }




        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );

        layoutParams.gravity = Gravity.BOTTOM|Gravity.RIGHT;

        if(isFist){
            isFist = false;
            windowManager.addView(inflate,layoutParams);
        }else {
            inflate.setVisibility(View.VISIBLE);
        }

    }

    private void resetProgressView(float y, int viewHeight) {
        if (y < 0) {
            y = 0;
        } else if (y > viewHeight) {
            y = viewHeight;
        }
        float progress = (viewHeight - y) / viewHeight;
        int height = (int) (progress * mMaxHeight);
        if (height > mMaxHeight) {
            height = mMaxHeight;
        } else if (height < mMinHeight) {
            height = mMinHeight;
        }
        mProgressView.setBackgroundResource(height > mNeedChangeProgress ?
                R.drawable.shape_efc638_10 : R.drawable.shape_efc638_10_bottom);
        mLayoutParams.height = height;
        mProgressView.setLayoutParams(mLayoutParams);
    }

    private AudioManager getAudioManager() {
        if (mAudioManager == null) {
            mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        }
        return mAudioManager;
    }

    /**
     * 调整音量
     * @param y
     * @param viewHeight
     */
    private void resetVolume(float y, int viewHeight) {
        float progress = (viewHeight - y) / viewHeight;
        int volume = (int) (progress * mMaxVolume + 0.5f);
        if (volume > mMaxVolume) {
            volume = mMaxVolume;
        } else if (volume < 1) {
            volume = 1;
        }
        if (mMap == null) {
            mMap = new HashMap<>();
        }

        mMap.put(EventBusEvent.BRIGHTNES_CHANGE, volume);
        EventBus.getDefault().post(mMap);

    }

    public void goneSound(){
        if(windowManager != null){
            if(inflate != null){
                inflate.setVisibility(View.GONE);
            }
        }

    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                resetProgressView(event.getY(), v.getHeight());
                resetVolume(event.getY(), v.getHeight());
                break;
        }
        return true;
    }
}
